package com.zt.bus.shiro.realm;

import com.monkeyk.os.oauth.shiro.OAuth2Token;
import com.monkeyk.os.service.OAuthRSService;
import com.zt.shiro.auth.StateToken;
import com.zt.shiro.realm.UserStateRealm;
import com.zt.user.util.OAuth2TokenUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by Administrator on 2017/6/9.
 */
public class BusStateRealm extends UserStateRealm {

    @Autowired
    private OAuthRSService rsService;

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof StateToken || token instanceof OAuth2Token;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
        if (authcToken instanceof StateToken) {// web登录
            return super.doGetAuthenticationInfo(authcToken);
        } else if (authcToken instanceof OAuth2Token) {// 通过oauth2请求
            return OAuth2TokenUtil.getSimpleAuthenticationInfo(rsService, authcToken, getName());
        } else {
            return super.doGetAuthenticationInfo(authcToken);
        }
    }

    public void setRsService(OAuthRSService rsService) {
        this.rsService = rsService;
    }
}
